﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace EasyTrader.Chart
{
    public partial class FutureMstBidsAsksChartFrm : System.Windows.Forms.Form
    {
        private EasyTrader.Signal.TotalSignalManager m_GSignalManager = null;
        public EasyTrader.Signal.TotalSignalManager GSignalManager
        {
            get { return m_GSignalManager; }
            set { m_GSignalManager = value; }
        }

        public FutureMstBidsAsksChartFrm()
        {
            InitializeComponent();
        }

        // Cybos Plus Data Set Member
        private ETDataSet m_CPDataSet = null;
        public EasyTrader.ETDataSet CPDataSet
        {
            get { return m_CPDataSet; }
            set { m_CPDataSet = value; }
        }
        private System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
        private void InitTimer()
        {
            timer.Interval = GlobalVar.ChartRefreshInterval;
            timer.Tick += new EventHandler(OnTimerTick);
            timer.Start();
        }

        // 여기서 차트를 갱신해 준다.
        private void OnTimerTick(object sender, EventArgs e)
        {
            // 차트에 데이터를 대응시켜 준다.
            if (GlobalVar.OperationMode == GlobalVar.OperationModeNormal)
            {
                SetChartData();
                ShowCurValue();
            }
            else
            {
                SetChartData(GlobalVar.SimulIndex);
                ShowCurValue(GlobalVar.SimulIndex);
            }
            SetForecastingYOnChart();
        }

        private void SetForecastingYOnChart()
        {
            if (m_GSignalManager == null)
                return;


            EasyTrader.Signal.ForecastingInfo foreInfo = GSignalManager.HogaBidsInfo;
            if (FutureMstBidsAsksChartCtrl.Series["매수잔량예상가"] != null && foreInfo != null)
            {
                FutureMstBidsAsksChartCtrl.Series["매수잔량예상가"].Points.Clear();
                FutureMstBidsAsksChartCtrl.Series["매수잔량예상가"].Points.AddXY(foreInfo.StartX, foreInfo.StartY);
                FutureMstBidsAsksChartCtrl.Series["매수잔량예상가"].Points.AddXY(foreInfo.EndX, foreInfo.EndY);
                txtBuyAngle.Text = "매수각도 : " + foreInfo.Angle.ToString("F2");
            }

            foreInfo = GSignalManager.HogaAsksInfo;
            if (FutureMstBidsAsksChartCtrl.Series["매도잔량예상가"] != null && foreInfo != null)
            {
                FutureMstBidsAsksChartCtrl.Series["매도잔량예상가"].Points.Clear();
                FutureMstBidsAsksChartCtrl.Series["매도잔량예상가"].Points.AddXY(foreInfo.StartX, foreInfo.StartY);
                FutureMstBidsAsksChartCtrl.Series["매도잔량예상가"].Points.AddXY(foreInfo.EndX, foreInfo.EndY);
                txtSellAngle.Text = "매도각도 : " + foreInfo.Angle.ToString("F2");
            }
        }

        private void ShowCurValue()
        {
            // 데이터 셋 객체가 없으면 아무일도 하지 않는다.
            if (m_CPDataSet == null || GlobalVar.FutureMstDataDownloaded == false)
                return;

            FutureMstTable dataTable = m_CPDataSet.GetFutureMstTable();
            int rowCount = dataTable.GetRowCount();
            if (rowCount == 0)
                return;

            DataRow row = dataTable.Rows[rowCount - 1];
            double bidsValue = Convert.ToDouble(row["매수총호가수량"]);
            double asksValue = Convert.ToDouble(row["매도총호가수량"]);
            double delta = bidsValue - asksValue;
            string curTime = row["시각"].ToString();
            if (curTime != "0")
            {
                string minText = curTime.Substring(curTime.Length - 2, 2);
                string hourText = curTime.Substring(0, curTime.Length - 2);

                txtCurTime.Text = "현재시간 " + hourText + " : " + minText;
                txtCurPrice.Text = "매수잔량 : " + bidsValue.ToString() + ", 매도잔량 : " + asksValue.ToString() + "  잔량차이 : " + delta.ToString();
            }
        }

        private void ShowCurValue(int a_CurIndex)
        {
            // 데이터 셋 객체가 없으면 아무일도 하지 않는다.
            if (m_CPDataSet == null)
                return;

            FutureMstTable dataTable = m_CPDataSet.GetFutureMstTable();
            int rowCount = dataTable.GetRowCount();
            if (rowCount == 0)
                return;
            if (rowCount - 1 < a_CurIndex)
                return;

            DataRow row = dataTable.Rows[a_CurIndex];
            double bidsValue = Convert.ToDouble(row["매수총호가수량"]);
            double asksValue = Convert.ToDouble(row["매도총호가수량"]);
            double delta = Math.Abs(bidsValue - asksValue);
            string curTime = row["시각"].ToString();
            if (curTime != "0")
            {
                string minText = curTime.Substring(curTime.Length - 2, 2);
                string hourText = curTime.Substring(0, curTime.Length - 2);

                txtCurTime.Text = "현재시간 " + hourText + " : " + minText;
                txtCurPrice.Text = "매수잔량 : " + bidsValue.ToString() + ", 매도잔량 : " + asksValue.ToString() + "  잔량차이 : " + delta.ToString();
            }
        }

        private void SetChartData()
        {
            // 데이터 셋 객체가 없으면 아무일도 하지 않는다.
            if (m_CPDataSet == null)
                return;

            FutureMstBidsAsksChartCtrl.Series["매도잔량"].Points.Clear();
            FutureMstBidsAsksChartCtrl.Series["매수잔량"].Points.Clear();
            FutureMstTable dataTable = m_CPDataSet.GetFutureMstTable();
            double maxValue = -10000.0;
            double minValue = 10000.0;
            int rowCount = dataTable.GetRowCount();
            if (rowCount == 0)
                return;

            int minIndex = 0;
            int maxIndex = 0;
            double bidsValue = 0;
            double asksValue = 0;
            for (int i = 0; i < rowCount; i++)
            {
                DataRow row = dataTable.Rows[i];
                try
                {
                    // 값이 없을 경우는 넘어 간다.
                    if (row["매수총호가수량"] == DBNull.Value)
                        continue;

                    bidsValue = Convert.ToDouble(row["매수총호가수량"]);
                    asksValue = Convert.ToDouble(row["매도총호가수량"]);
                    FutureMstBidsAsksChartCtrl.Series["매수잔량"].Points.AddXY(i, bidsValue);
                    FutureMstBidsAsksChartCtrl.Series["매도잔량"].Points.AddXY(i, asksValue);
                    FutureMstBidsAsksChartCtrl.Series["매수잔량"].Points[i].XValue = i;
                    int hour = Convert.ToInt32(row["시각"]);
                    if (hour != 0)
                        FutureMstBidsAsksChartCtrl.Series["매수잔량"].Points[i].AxisLabel = row["시각"].ToString();
                    else
                        FutureMstBidsAsksChartCtrl.Series["매수잔량"].Points[i].AxisLabel = GlobalVar.HogaDayIndexToTime(i).ToString();
                    

                    if (bidsValue != 0 && bidsValue > maxValue)
                    {
                        maxValue = bidsValue;
                        maxIndex = i;
                    }

                    if (asksValue != 0 && asksValue > maxValue)
                    {
                        maxValue = asksValue;
                        maxIndex = i;
                    }

                    if (asksValue != 0 && asksValue < minValue)
                    {
                        minValue = asksValue;
                        minIndex = i;
                    }

                    if (bidsValue != 0 && bidsValue < minValue)
                    {
                        minValue = bidsValue;
                        minIndex = i;
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
            }

            int valueHeight = (int)maxValue / 2500;
            maxValue = valueHeight * 2500 + 2500;
            //minValue = 0;

            if (maxValue >= minValue)
            {
                FutureMstBidsAsksChartCtrl.ChartAreas["BidsAsks"].AxisY.Minimum = minValue;
                FutureMstBidsAsksChartCtrl.ChartAreas["BidsAsks"].AxisY.Maximum = maxValue;
            }
        }

        private void SetChartData(int a_CurIndex)
        {
            // 데이터 셋 객체가 없으면 아무일도 하지 않는다.
            if (m_CPDataSet == null)
                return;

            FutureMstBidsAsksChartCtrl.Series["매도잔량"].Points.Clear();
            FutureMstBidsAsksChartCtrl.Series["매수잔량"].Points.Clear();
            FutureMstTable dataTable = m_CPDataSet.GetFutureMstTable();
            double maxValue = -10000.0;
            double minValue = 10000.0;
            int rowCount = dataTable.GetRowCount();
            if (rowCount == 0)
                return;
            if (rowCount - 1 < a_CurIndex)
                return;

            int minIndex = 0;
            int maxIndex = 0;
            double bidsValue = 0;
            double asksValue = 0;
            for (int i = 0; i <= a_CurIndex; i++)
            {
                DataRow row = dataTable.Rows[i];
                try
                {
                    // 값이 없을 경우는 넘어 간다.
                    if (row["매수총호가수량"] == DBNull.Value)
                        continue;

                    bidsValue = Convert.ToDouble(row["매수총호가수량"]);
                    asksValue = Convert.ToDouble(row["매도총호가수량"]);
                    FutureMstBidsAsksChartCtrl.Series["매수잔량"].Points.AddXY(i, bidsValue);
                    FutureMstBidsAsksChartCtrl.Series["매도잔량"].Points.AddXY(i, asksValue);
                    FutureMstBidsAsksChartCtrl.Series["매수잔량"].Points[i].XValue = i;
                    int hour = Convert.ToInt32(row["시각"]);
                    if (hour != 0)
                        FutureMstBidsAsksChartCtrl.Series["매수잔량"].Points[i].AxisLabel = row["시각"].ToString();
                    else
                        FutureMstBidsAsksChartCtrl.Series["매수잔량"].Points[i].AxisLabel = GlobalVar.HogaDayIndexToTime(i).ToString();


                    if (bidsValue != 0 && bidsValue > maxValue)
                    {
                        maxValue = bidsValue;
                        maxIndex = i;
                    }

                    if (asksValue != 0 && asksValue > maxValue)
                    {
                        maxValue = asksValue;
                        maxIndex = i;
                    }

                    if (asksValue != 0 && asksValue < minValue)
                    {
                        minValue = asksValue;
                        minIndex = i;
                    }

                    if (bidsValue != 0 && bidsValue < minValue)
                    {
                        minValue = bidsValue;
                        minIndex = i;
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
            }

            int valueHeight = (int)maxValue / 2500;
            maxValue = valueHeight * 2500 + 2500;
            //minValue = 0;

            if (maxValue >= minValue)
            {
                FutureMstBidsAsksChartCtrl.ChartAreas["BidsAsks"].AxisY.Minimum = minValue;
                FutureMstBidsAsksChartCtrl.ChartAreas["BidsAsks"].AxisY.Maximum = maxValue;
            }
        }

        public FutureMstBidsAsksChartFrm(ETDataSet a_DataSet)
        {
            InitializeComponent();
            m_CPDataSet = a_DataSet;
            InitControl();
        }

        private void InitControl()
        {
            cbFore.Checked = true;
            FutureMstBidsAsksChartCtrl.Series["매수잔량"].ToolTip = "#VALX : #VAL";
            FutureMstBidsAsksChartCtrl.Series["매도잔량"].ToolTip = "#VALX : #VAL";
            FutureMstBidsAsksChartCtrl.Series["매수잔량예상가"].ToolTip = "#VALX : #VAL";
            FutureMstBidsAsksChartCtrl.Series["매도잔량예상가"].ToolTip = "#VALX : #VAL";
        }

        private void InitChart()
        {
            FutureMstBidsAsksChartCtrl.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(211)), ((System.Byte)(223)), ((System.Byte)(240)));
            FutureMstBidsAsksChartCtrl.BackSecondaryColor = System.Drawing.Color.White;
            FutureMstBidsAsksChartCtrl.BackGradientStyle = System.Windows.Forms.DataVisualization.Charting.GradientStyle.TopBottom;
            FutureMstBidsAsksChartCtrl.BorderlineColor = System.Drawing.Color.FromArgb(((System.Byte)(26)), ((System.Byte)(59)), ((System.Byte)(105)));
            FutureMstBidsAsksChartCtrl.BorderlineDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Solid;
            FutureMstBidsAsksChartCtrl.BorderlineWidth = 2;
            FutureMstBidsAsksChartCtrl.BorderSkin.SkinStyle = System.Windows.Forms.DataVisualization.Charting.BorderSkinStyle.Emboss;

            FutureMstBidsAsksChartCtrl.ChartAreas["BidsAsks"].AxisX.MajorGrid.Interval = 20;
            FutureMstBidsAsksChartCtrl.ChartAreas["BidsAsks"].AxisX.Interval = 20;
            FutureMstBidsAsksChartCtrl.ChartAreas["BidsAsks"].AxisY.MajorGrid.Interval = 5000;
            FutureMstBidsAsksChartCtrl.ChartAreas["BidsAsks"].AxisY.Interval = 2500;
            FutureMstBidsAsksChartCtrl.ChartAreas["BidsAsks"].AxisY.MinorGrid.Interval = 2500;

            FutureMstBidsAsksChartCtrl.ChartAreas["BidsAsks"].Area3DStyle.IsClustered = true;
            FutureMstBidsAsksChartCtrl.ChartAreas["BidsAsks"].Area3DStyle.Perspective = 10;
            FutureMstBidsAsksChartCtrl.ChartAreas["BidsAsks"].Area3DStyle.IsRightAngleAxes = false;
            FutureMstBidsAsksChartCtrl.ChartAreas["BidsAsks"].Area3DStyle.WallWidth = 0;
            FutureMstBidsAsksChartCtrl.ChartAreas["BidsAsks"].Area3DStyle.Inclination = 15;
            FutureMstBidsAsksChartCtrl.ChartAreas["BidsAsks"].Area3DStyle.Rotation = 10;
            FutureMstBidsAsksChartCtrl.ChartAreas["BidsAsks"].AxisX.IsLabelAutoFit = true;
            FutureMstBidsAsksChartCtrl.ChartAreas["BidsAsks"].AxisX.LabelStyle.Font = new System.Drawing.Font("Trebuchet MS", 8.25F, System.Drawing.FontStyle.Bold);
            FutureMstBidsAsksChartCtrl.ChartAreas["BidsAsks"].AxisX.LabelStyle.IsEndLabelVisible = false;
            FutureMstBidsAsksChartCtrl.ChartAreas["BidsAsks"].AxisX.LineColor = System.Drawing.Color.FromArgb(((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)));
            FutureMstBidsAsksChartCtrl.ChartAreas["BidsAsks"].AxisX.MajorGrid.LineColor = System.Drawing.Color.FromArgb(((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)));
            FutureMstBidsAsksChartCtrl.ChartAreas["BidsAsks"].AxisY.IsLabelAutoFit = true;
            FutureMstBidsAsksChartCtrl.ChartAreas["BidsAsks"].AxisY.LabelStyle.Font = new System.Drawing.Font("Trebuchet MS", 8.25F, System.Drawing.FontStyle.Bold);
            FutureMstBidsAsksChartCtrl.ChartAreas["BidsAsks"].AxisY.LineColor = System.Drawing.Color.FromArgb(((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)));
            FutureMstBidsAsksChartCtrl.ChartAreas["BidsAsks"].AxisY.MajorGrid.LineColor = System.Drawing.Color.FromArgb(((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)));
            FutureMstBidsAsksChartCtrl.ChartAreas["BidsAsks"].AxisY.IsStartedFromZero = false;
            FutureMstBidsAsksChartCtrl.ChartAreas["BidsAsks"].AxisY.MinorGrid.LineColor = System.Drawing.Color.FromArgb(((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)));
            FutureMstBidsAsksChartCtrl.ChartAreas["BidsAsks"].AxisY.MinorGrid.LineDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.DashDot;
            //FutureMstBidsAsksChartCtrl.ChartAreas["Price"].AxisY.LabelAutoFitMaxFontSize;
            FutureMstBidsAsksChartCtrl.ChartAreas["BidsAsks"].BackColor = System.Drawing.Color.FromArgb(((System.Byte)(64)), ((System.Byte)(165)), ((System.Byte)(191)), ((System.Byte)(228)));
            FutureMstBidsAsksChartCtrl.ChartAreas["BidsAsks"].BackSecondaryColor = System.Drawing.Color.White;
            FutureMstBidsAsksChartCtrl.ChartAreas["BidsAsks"].BackGradientStyle = System.Windows.Forms.DataVisualization.Charting.GradientStyle.TopBottom;
            FutureMstBidsAsksChartCtrl.ChartAreas["BidsAsks"].BorderColor = System.Drawing.Color.FromArgb(((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)));
            FutureMstBidsAsksChartCtrl.ChartAreas["BidsAsks"].BorderDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Solid;
            FutureMstBidsAsksChartCtrl.ChartAreas["BidsAsks"].Position.Auto = true;
            //FutureMstBidsAsksChartCtrl.ChartAreas["Price"].Position.Height = 80F;
            //FutureMstBidsAsksChartCtrl.ChartAreas["Price"].Position.Width = 80F;
            //FutureMstBidsAsksChartCtrl.ChartAreas["Price"].Position.X = 5F;
            //FutureMstBidsAsksChartCtrl.ChartAreas["Price"].Position.Y = 10F;
            FutureMstBidsAsksChartCtrl.ChartAreas["BidsAsks"].ShadowColor = System.Drawing.Color.Transparent;
            
            //FutureMstBidsAsksChartCtrl.Series["지수"].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
             

            //FutureMstBidsAsksChartCtrl.Series[0]["PriceUpColor"] = "blue";
            //FutureMstBidsAsksChartCtrl.Series[0]["PriceDownColor"] = "Red";
            //FutureMstBidsAsksChartCtrl.ChartAreas["Price"].AxisX.LabelStyle.IsEndLabelVisible = true;
            //FutureMstBidsAsksChartCtrl.ChartAreas["Price"].AxisX.IsMarginVisible = true;
            //FutureMstBidsAsksChartCtrl.ChartAreas["Price"].AxisY.LabelStyle.IsEndLabelVisible = true;
            //FutureMstBidsAsksChartCtrl.ChartAreas["Price"].AxisY.IsMarginVisible = true;
            //FutureMstBidsAsksChartCtrl.Series[0].Color = Color.Red;
            //DataPointCustomProperties aa = FutureMstBidsAsksChartCtrl.Series[0]["PriceUpColor"];

            //System.Windows.Forms.DataVisualization.Charting.StripLine stripLine = new System.Windows.Forms.DataVisualization.Charting.StripLine();
            //stripLine.Interval = 0;
            //stripLine.IntervalOffset = GlobalVar.MinBidAsk;
            //stripLine.BackColor = Color.Black;
            //stripLine.StripWidth = 1.0;
            //stripLine.Text = "임계선";
            //stripLine.TextAlignment = StringAlignment.Near;
            //FutureMstBidsAsksChartCtrl.ChartAreas[0].AxisY.StripLines.Add(stripLine);
        }

        private void FutureMstBidsAsksChart_Load(object sender, EventArgs e)
        {
            InitChart();
            InitTimer();
        }

        private void FutureMstBidsAsksChart_FormClosing(object sender, FormClosingEventArgs e)
        {
            timer.Stop();
        }

        private void cbFore_CheckedChanged(object sender, EventArgs e)
        {
            if (cbFore.Checked == true)
            {
                FutureMstBidsAsksChartCtrl.Series["매수잔량예상가"].Enabled = true;
                FutureMstBidsAsksChartCtrl.Series["매도잔량예상가"].Enabled = true;
            }
            else
            {
                FutureMstBidsAsksChartCtrl.Series["매수잔량예상가"].Enabled = false;
                FutureMstBidsAsksChartCtrl.Series["매도잔량예상가"].Enabled = false;
            }
        }
    }
}
